﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dao;
using System.Resources;
using common;
using System.Globalization;
namespace GTVAccountingSystem.view.usercontrol
{
    public partial class uclUserInformation : UserControl
    {
        UsersDAO usersDAO = new UsersDAO();
        UserGroupDAO userGroupDAO = new UserGroupDAO();
        DataTable dt, dtUsersNoGroup, dtUsersForGroup;
        ResourceManager rm = Constant.RM;
        CultureInfo culture = Constant.CULTURE;
        usercontrol.uclUsersDetailTab userDetailTab;
        usercontrol.uclGroupsDetailTab groupDetailTab;
        int countRow = 0;
        public uclUserInformation()
        {
            InitializeComponent();
            getTabDependOnUserOrGroup();
        }
        public void loadTableUser()
        {
            dt = usersDAO.getAllUser();
            dt.Columns["StaffCode"].ColumnName = rm.GetString("lblStaffCode", culture);
            dt.Columns["Username"].ColumnName = rm.GetString("lblUsername", culture);
            dt.Columns["FirstName"].ColumnName = rm.GetString("lblFullName", culture);
            /*dt.Columns["LastName"].ColumnName = rm.GetString("clnStaffCode", culture);
            dt.Columns["Email"].ColumnName = rm.GetString("lblEmail", culture);
            dt.Columns["Phone1"].ColumnName = rm.GetString("lblPhone", culture) + " 1";
            dt.Columns["Phone2"].ColumnName = rm.GetString("lblPhone", culture) + " 2";
            dt.Columns["Address"].ColumnName = rm.GetString("lblAddress", culture);
            dt.Columns["CountLogin"].ColumnName = rm.GetString("lblCountLogin", culture);
            dt.Columns["Status"].ColumnName = rm.GetString("lblStatus", culture);
            dt.Columns["CompanyName"].ColumnName = rm.GetString("lblCompanyName", culture);*/
            dgvUsers.DataSource = dt;
            dgvUsers.Columns.Remove("IDLogin");
        }
        public void loadTableUserGroup()
        {
            dt = userGroupDAO.getAllUserGroup();
            dgvGroups.DataSource = dt;
            if (dgvGroups.ColumnCount == 0)
            {
                dgvGroups.Columns["GroupName"].Name = rm.GetString("clnGroupName", culture);
                dgvGroups.Columns.Remove("ID");
                dgvGroups.Columns.Remove("Descriptions");
            }
            if (dt.Rows.Count > 0)
                loadTableUsersNoGroupAndUsersForGroup(dt.Rows[0]["IDGroup"].ToString());
        }

        private void dgvUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            userDetailTab.getDataFromUserInformation(dt.Rows[e.RowIndex]);
        }
        public void getTabDependOnUserOrGroup()
        {
            int tabIndex = tabControl1.SelectedIndex;
            ControlCollection controls = this.splitContainer1.Panel2.Controls;
            controls.Clear();
            if (tabIndex == 0)
            {
                loadTableUser();
                userDetailTab = new usercontrol.uclUsersDetailTab();
                userDetailTab.getDataFromUserInformation(dt.Rows[0]);
                controls.Add(userDetailTab);
                userDetailTab.Dock = DockStyle.Fill;
            }
            else
            {
                loadTableUserGroup();
                groupDetailTab = new usercontrol.uclGroupsDetailTab();
                if (dt.Rows.Count > 0)
                    groupDetailTab.getDataFromUserGroup(dt.Rows[0]);
                controls.Add(groupDetailTab);
                groupDetailTab.Dock = DockStyle.Fill;
            }
        }
        private void dgvUsers_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dt.DefaultView.Sort = dgvUsers.Columns[e.ColumnIndex].Name + " ASC";
        }
        private void dgvGroups_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dt.DefaultView.Sort = dgvGroups.Columns[e.ColumnIndex].Name + " ASC";
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            getTabDependOnUserOrGroup();
        }

        private void dgvUsers_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            //userDetailTab.getDataFromUserInformation(dt.Rows[e.]);
        }

        private void dgvUsers_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {

        }
        public void loadTableUsersNoGroupAndUsersForGroup(String idGroup)
        {
            DataTable[] dts = usersDAO.getUsersNoGroupAndUsersByGroupID(idGroup);
            dtUsersNoGroup = dts[0];
            dtUsersForGroup = dts[1];
            dgvUsersNoGroup.DataSource = dtUsersNoGroup;
            dgvUsersNoGroup.Columns.Remove("ID");
            dgvUsersForGroup.DataSource = dtUsersForGroup;
            dgvUsersForGroup.Columns.Remove("ID");
        }
        public void addUserIntoGroup()
        {
            if (dtUsersNoGroup.Rows.Count > 0)
            {
                int index = dgvUsersNoGroup.Rows.IndexOf(dgvUsersNoGroup.SelectedRows[0]);
                DataRow dtRow = dtUsersNoGroup.Rows[index];
                if (usersDAO.addUserIntoGroup(dtRow["ID"].ToString(), dt.Rows[0]["IDGroup"].ToString()))
                {
                    dtUsersForGroup.Rows.InsertAt(dtRow, dtUsersForGroup.Rows.Count);
                    dtUsersNoGroup.Rows.RemoveAt(index);
                }
            }
            else
                MessageBox.Show("No Users for add group");
        }
        public void removeGroupOfUser()
        {
            /*int index = dgvUsersForGroup.Rows.IndexOf(dgvUsersForGroup.SelectedRows[0]);
            DataRow dtRow = dtUsersForGroup.Rows[index];
            if (usersDAO.removeGroupOfUser(dtRow["IDInformations"].ToString()))
            {
                dtUsersNoGroup.Rows.InsertAt(dtRow, dtUsersNoGroup.Rows.Count);
                dtUsersForGroup.Rows.RemoveAt(index);
            }*/
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            dt.Rows.InsertAt(dt.NewRow(), 0);
            countRow++;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Boolean check = true;
            for (int i = 0; i < countRow; i++)
            {
                DataGridViewCellCollection cells = dgvGroups.Rows[i].Cells;
                if(!userGroupDAO.insertUserGroup(cells[0].ToString(), cells[1].ToString()))
                {
                    check = false;
                }
            }
            if(check)
                MessageBox.Show("Insert group successful");
        }

        private void btnAddUserIntoGroup_Click(object sender, EventArgs e)
        {
            addUserIntoGroup();
        }

        private void btnRemoveUser_Click(object sender, EventArgs e)
        {
            removeGroupOfUser();
        }
    }
}
